package base_grammar

import "fmt"

// 切片为可变长度数组

func copyTest() {
	var s1 = []int{1, 2, 3, 4, 5}
	s2 := s1 // 复制是将地址赋值  s1修改 会影响s2
	s3 := make([]int, 5)
	copy(s3, s1)
	s1[0] = 100

	fmt.Printf("s1: %v\n", s1)
	fmt.Printf("s2: %v\n", s2)
	fmt.Printf("s3: %v\n", s3)

}

func slice_query() {
	var s1 = []int{1, 2, 3, 4, 5}
	value := 3
	for idx, v := range s1 {
		if v == value {
			fmt.Printf("i=%v", idx)
		}
	}
}

func slice_update() {
	var s1 = []int{1, 2, 3, 4, 5}
	s1[1] = 200
	fmt.Printf("s1: %v\n", s1)
}

func slice_delete() {
	// 删掉索引为2的元素
	var s1 = []int{1, 2, 3, 4, 5}
	s2 := append(s1[:2], s1[3:]...)
	fmt.Printf("s2: %v\n", s2)
}

func slice_add() {
	var s1 = []int{1, 2, 3, 4, 5}
	s1 = append(s1, 100)
	s1 = append(s1, 200)
	s1 = append(s1, 300)
	fmt.Printf("s1: %v\n", s1)
}

func slice_f3() {
	var a1 = [...]int{1, 2, 3, 4, 5, 6}
	a2 := a1[:]
	fmt.Printf("s2: %v\n", a2)
	a3 := a1[3:]
	fmt.Printf("s3: %v\n", a3)
	a4 := a1[:5]
	fmt.Printf("s4: %v\n", a4)
	a5 := a1[3:5]
	fmt.Printf("s5: %v\n", a5)
}

func slice_f2() {
	var s1 = []int{1, 2, 3, 4, 5, 6}
	s2 := s1[:]
	fmt.Printf("s2: %v\n", s2)
	s3 := s1[3:]
	fmt.Printf("s3: %v\n", s3)
	s4 := s1[:5]
	fmt.Printf("s4: %v\n", s4)
	s5 := s1[3:5]
	fmt.Printf("s5: %v\n", s5)
}

func slice_f1() {
	var s = []int{1, 2}
	var s1 []string
	fmt.Printf("s: %v\n", s)
	fmt.Printf("s1: %v\n", s1)
	s3 := make([]bool, 2)
	fmt.Printf("s3: %v\n", s3)

	var s2 = []int{1, 2, 3}
	fmt.Printf("len(s2): %v\n", len(s2))
	fmt.Printf("cap(s2): %v\n", cap(s2))
	fmt.Printf("s2[0]: %v\n", s2[0])

}
